
#include "Command.h"
#include "../engine/Server.h"

using namespace Engine;

namespace Server {
namespace Command {

class CServerCommand : public CCommandAction {

	void ShowSessions(string& output) {
		
		CPackage::JsonBuffer ss;
		CPackage::JsonWriter writer(ss);
		CServer::GetInstance()->GetDescOfSessions(writer);

		output = ss.GetString();
	}

	void ShowSession(int sid, string& output) {
		SessionRef s = CServer::GetInstance()->GetSession(sid);
		if(s) {
			CPackage::JsonBuffer ss;
			CPackage::JsonWriter writer(ss);
			s->GetDesc(writer);
			output = ss.GetString();
		}
	}

	void ShowDevice(int sid, int dev_id, string& output) {
		SessionRef s = CServer::GetInstance()->GetSession(sid);
		if(s) {
			DeviceRef dev = s->GetDevice(dev_id);
			if(dev) {
				CPackage::JsonBuffer ss;
				CPackage::JsonWriter writer(ss);
				dev->GetDesc(writer);
				output = ss.GetString();
			}
		}
	}

public :

	CServerCommand() {
		CCommandTreeNode::mCommandRoot.AddChild(
			string("session <>"),
			this
			);
		CCommandTreeNode::mCommandRoot.AddChild(
			string("session <> device <>"),
			this
			);
	}

	virtual void DoAction(vector<CCommandParam>& params, string& output)
	{
		if(params.size() == 0) {
			ShowSessions(output);
		}
		else
		if(params.size() == 1) {
			ShowSession(params[0].GetInt(), output);
		}
		else
		if(params.size() == 2) {
			ShowDevice(params[0].GetInt(), params[1].GetInt(), output);
		}
	}
};

static CServerCommand sServerCommand;

} // Server
} // Command